package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import utility.Record;

import business.SalleBL;
import business.SeanceBL;
import business.SeanceSpecialeBL;
import business.TypeEnseignementBL;


public class FrameAjoutSeanceSpeciale extends JFrame {
	
	//champs du formulaire
	private JPanel general;
    private JButton save;
    private JButton annuler;
	private JTextField fieldDesc;

  //variables d'affichage du formulaire
	private int idSeance = -1;
    private int idSalle = 1;
    private int idPeriode;
        
	public void drawCommandBox(JPanel commandBox) {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JSeparator(), BorderLayout.PAGE_START);
        buttonPanel.add(commandBox, BorderLayout.LINE_END);
        
        general.add(buttonPanel, BorderLayout.SOUTH);
	}
	

	public FrameAjoutSeanceSpeciale(int idSean, int idSa, int idPer) {
		
		idSeance = idSean;
		idSalle = idSa;
		idPeriode = idPer;
		
    	setSize(500, 300);
		setTitle("Ajouter une séance");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder("Création d'une séance spéciale"),
		BorderFactory.createEmptyBorder(5,5,5,5)));

        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
        JLabel labelNom = new JLabel("Description:", JLabel.TRAILING);
        p.add(labelNom);
		fieldDesc = new JTextField("");
        labelNom.setLabelFor(fieldDesc);
        p.add(fieldDesc);
        
        //Lay out the panel
        SpringUtilities.makeCompactGrid(p,
                                        1, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.setActionCommand("Enregistrer");
        save.addActionListener(new FrameAjoutSeanceSpecialeListener());
        commandBox.add(save);
        annuler = new JButton("Annuler");
        annuler.setActionCommand("Annuler");
        annuler.addActionListener(new FrameAjoutSeanceSpecialeListener());
        commandBox.add(annuler);
        drawCommandBox(commandBox);

	}

	public String getDesc() {
		return fieldDesc.getText();
	}
	
	private class FrameAjoutSeanceSpecialeListener implements ActionListener {
	
		public void actionPerformed(ActionEvent e) {
			System.out.println("Action");		
			if (e.getActionCommand().equals("Enregistrer")) {
				System.out.println("Demande d'enregistrement");
				SeanceSpecialeBL seanceSave = new SeanceSpecialeBL();
				// si -1, c'est un ajout d'un type heure, sinon modification du
				// type heure de l'id concerné
				if (idSeance == -1) {
					System.out.println("ici");
					seanceSave.ajouterSeanceSpeciale(idSeance, getDesc(), idPeriode, idSalle);
				} else {
					System.out.println("la");
					seanceSave.modifierSeanceSpeciale(idSeance, getDesc(), idPeriode, idSalle);
				}
				dispose();
			} else if (e.getActionCommand().equals("Annuler")) {
				System.out.println("annulation");
				dispose();
			}
		}
	}

}
